package cn.edu.huanghuai.newsofhuanghuai.Controller;

import cn.edu.huanghuai.newsofhuanghuai.common.Result;
import cn.edu.huanghuai.newsofhuanghuai.pojo.User;
import cn.edu.huanghuai.newsofhuanghuai.sevice.UserService;
import cn.edu.huanghuai.newsofhuanghuai.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureTestEntityManager;
import org.springframework.web.bind.annotation.*;

/**
 * user:liguo
 * date:2025/6/10
 * suaige
 */
@RestController
@CrossOrigin
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/add")
    public Result add(User user) {
        int result = userService.addUser(user);
        // 表示的是 增加成功
        if(result > 0 ) {
            return new Result(200, "增加成功");
        } else {
            return new Result(403, "增加失败");
        }
    }


    @GetMapping("/findById/{userId}")
    public Result finById(@PathVariable Integer userId) {

        System.out.println(userId);
        User user = userService.getUser(userId);

        if(user != null) {
            // 把数据带给前端
            return new Result(200,"查询成功", user);
        }else {
            return new Result(403, "查询错误");
        }
    }


    @GetMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        int result = userService.deleteUser(id);
        if(result > 0 ) {
            return new Result(200, "删除成功");
        } else {
            return new Result(403, "删除失败");
        }
    }


    /**
     * 根据用户的id进行更新的
     * @param user
     * @return
     */
    @PostMapping("/updateById")
    public Result updateById(@RequestBody User user) {
        int result = userService.updateUser(user);
        if(result > 0 ) {
            return new Result(200, "更改成功");
        } else {
            return new Result(403, "更改失败");
        }
    }

    @GetMapping("/queryAll")
    public Result queryAll() {
        return new Result(200, "查询成功", userService.queryAllUser());
    }

    @GetMapping("/banUser")
    public Result banUser(int id) {
        int result = userService.banUser(id);
        if(result > 0 ) {
            return new Result(200, "封禁成功");
        } else {
            return new Result(403, "封禁失败");
        }
    }

    @GetMapping("/releaseUser")
    public Result releaseUser(int id) {
        int result = userService.releaseUser(id);
        if(result > 0 ) {
            return new Result(200, "解封成功");
        } else {
            return new Result(403, "解封失败");
        }
    }


    /**
     * 用户的登录操作
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        User userByName = userService.queryUserByName(user.getUsername());
        if(userByName != null) {
            // 对密码进行加密 测试MD5
            if(Md5Util.checkPassword(user.getPassword(), userByName.getPassword())) {
                if(userByName.getState() == 0) {
                    return new Result(403, "您被封号了", userByName);
                } else {
                    return new Result(200, "欢迎回来", userByName);
                }
            } else {
                return new Result(403, "密码不正确");
            }
        } else {
            return new Result(403, "该用户不存在");
        }
    }


    /**
     * 注册新的用户，注意对密码实行MD5加密
     * @param user
     * @return
     */
    @PostMapping("/register")
    private Result register(@RequestBody User user) {
        // 注册之前先看看这个用户名是不是已经存在的
        User userByName = userService.queryUserByName(user.getUsername());
        if(userByName != null) {
            return new Result(403, "改用户名已存在，换一个吧");
        }
        // 对密码进行加码
        user.setPassword(Md5Util.getMD5String(user.getPassword()));
        int result = userService.registerUser(user);
        if(result > 0 ) {
            return new Result(200, "注册成功，快去登录吧");
        } else {
            return new Result(403, "用户注册失败");
        }
    }


}
